/*
 * Copyright 2008-now Nobject.org, All Rights Reserved.
 * 
 * This software is licensed under LGPL license.
 * See terms of license at gnu.org.
 * 
 * For more information,visit:
 * 
 * http://www.nobject.org
 */
package org.nobject.common.code.model.javascript;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Js Model class
 * 
 * EXP:
 * 	JsModel js=new JsModel();
 *	js.setJsComment			("用户管理JS");
 *	js.setJsCommentAuthor	("卞荣军");
 *	js.setJsCommentVersion	("0.0");
 *	
 *	ParamModel param=new ParamModel("name","名称");
 *	FunctionModel function=new FunctionModel("addUser","添加用户");
 *	function.addParam(param);
 *	js.addFunction(function);
 *	
 *	BlockModel block=new BlockModel("window.onload=addUser;");
 *	js.addBlock(block);
 *	VariableModel variable=new VariableModel("curUser",null,"当前用户");
 *	js.addVariable(variable);
 *	System.out.println(js.generateJsString());
 * 
 * @author bianrongjun
 * @version 1.0
 */
public class JsModel {
	
	/**
	 * jsName
	 */
	private String jsName;
	
	/**
	 * JS Comment
	 */
	private String jsComment;

	/**
	 * JS CommentAuthor
	 */
	private String jsCommentAuthor;

	/**
	 * JS CommentVersion
	 */
	private String jsCommentVersion;

	/**
	 * Variables
	 */
	private Map variables = new LinkedHashMap();

	/**
	 * Functions
	 */
	private Map functions = new LinkedHashMap();

	/**
	 * Blocks
	 */
	private List blocks = new LinkedList();

	/**
	 * getJsName
	 */
	public String getJsName() {
		return jsName;
	}

	/**
	 * setJsName
	 * @param jsName
	 */
	public void setJsName(String jsName) {
		this.jsName = jsName;
	}

	/**
	 * AddVariable
	 * 
	 * @param variable
	 */
	public void addVariable(VariableModel variable) {
		this.variables.put(variable.getVariableName(), variable);
	}

	/**
	 * AddFunction
	 * 
	 * @param function
	 */
	public void addFunction(FunctionModel function) {
		this.functions.put(function.getFunctionName(), function);
	}

	/**
	 * AddBlock
	 * 
	 * @param block
	 */
	public void addBlock(BlockModel block) {
		this.blocks.add(block);
	}

	/**
	 * Generate JS String
	 * 
	 * @return
	 */
	public String generateJsString() {

		StringBuffer sb = new StringBuffer();

		sb.append(generateCommentString());

		for (Iterator iterator = variables.keySet().iterator(); iterator.hasNext();) {
			String key=(String)iterator.next();
			VariableModel variable = (VariableModel) variables.get(key);
			sb.append("\n" + variable.generateVariableString() + "\n");
		}

		for (Iterator iterator = functions.keySet().iterator(); iterator.hasNext();) {
			String key=(String)iterator.next();
			FunctionModel function = (FunctionModel) functions.get(key);
			sb.append("\n" + function.generateFunctionString() + "\n");
		}

		for (Iterator iterator = blocks.iterator(); iterator.hasNext();) {
			BlockModel block = (BlockModel) iterator.next();
			sb.append("\n" + block.generateBlockString() + "\n");
		}

		return sb.toString();

	}

	/**
	 * Generate Comment String
	 * 
	 * @return
	 */
	private String generateCommentString() {
		StringBuffer sb = new StringBuffer();

		if (this.jsComment != null) {
			sb.append(" * "+this.jsComment + "\n");
		}

		if (this.jsCommentAuthor != null) {
			sb.append(" * @author " + this.jsCommentAuthor + "\n");
		}

		if (this.jsCommentVersion != null) {
			sb.append(" * @version " + this.jsCommentVersion + "\n");
		}

		if (!sb.toString().equals("")) {
			return ("/*\n" + sb.toString() + " */\n");
		} else {
			return "";
		}
	}

	/**
	 * getJsComment
	 * @return
	 */
	public String getJsComment() {
		return jsComment;
	}

	/**
	 * setJsComment
	 * @param jsComment
	 */
	public void setJsComment(String jsComment) {
		this.jsComment = jsComment;
	}

	/**
	 * getJsCommentAuthor
	 * @return
	 */
	public String getJsCommentAuthor() {
		return jsCommentAuthor;
	}

	/**
	 * setJsCommentAuthor
	 * @param jsCommentAuthor
	 */
	public void setJsCommentAuthor(String jsCommentAuthor) {
		this.jsCommentAuthor = jsCommentAuthor;
	}

	/**
	 * getJsCommentVersion
	 * @return
	 */
	public String getJsCommentVersion() {
		return jsCommentVersion;
	}

	/**
	 * 设置JS注释
	 * @param jsCommentVersion
	 */
	public void setJsCommentVersion(String jsCommentVersion) {
		this.jsCommentVersion = jsCommentVersion;
	}
}
